<template>
  <el-container>
    <el-header>
      <el-row :gutter="10">
        <el-col :span="4">
          <div class="grid-content bg-purple" style="height:60px">
            <img src="/static/logo.png" alt>
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple title">电商后台管理系统</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple logout">
            <el-button @click="logout" type="warning">退出</el-button>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '174px'">
        <!-- 折叠图标 -->
        <!-- 添加折叠div -->
        <div @click="toggleMenu" class="toggle-button">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="menuItem.id+''" v-for="menuItem in menuList" :key="menuItem.id">
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="iconsObj[menuItem.id]"></i>
              <!-- 一级菜单的文本 -->
              <span>{{menuItem.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              @click="saveNavState(item.path)"
              :index="item.path"
              v-for="item in menuItem.children"
              :key="item.id"
            >
              <template slot="title">
                <!-- 二级菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单的文本 -->
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 二级路由 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuListApi } from '@/api/privilege'
export default {
  name: 'HomeVue',
  data() {
    return {
      // 菜单栏权限数据
      menuList: [],
      // 一级菜单的图标:让一级菜单的id和子图图标建立关联关系
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制菜单栏是否折叠，默认为false代表不折叠
      isCollapse: false,
      // 此时被激活的菜单栏地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    // 从缓存中获取上一次的激活状态
    this.activePath = sessionStorage.getItem('path')
  },
  methods: {
    // 保存激活时的菜单地址
    saveNavState(path) {
      this.activePath = path
      // 并且把当前的激活状态保存起来，方便下次刷新页面还能到当前的激活状态下
      sessionStorage.setItem('path', path)
    },
    // 菜单的折叠
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },
    // 获取菜单栏数据
    async getMenuList() {
      const res = await getMenuListApi()
      // console.log(res);
      this.menuList = res
    },
    // 退出
    logout() {
      // 清除token
      window.sessionStorage.clear()
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
  .el-header {
    background-color: #d3dce6;
    color: #333;
    line-height: 60px;
    padding-left: 0;
    .title {
      color: white;
      font-size: 24px;
      text-align: center;
    }
    .logout {
      text-align: right;
    }
  }
  .el-aside {
    background-color: #333744;
    color: #333;
    text-align: left;
    line-height: 200px;
    .el-menu {
      border-right: 0;
      .iconfont {
        margin-right: 8px;
      }
    }
    // 按钮样式
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.3em;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
    color: #333;
  }
}
</style>
